package main

import (
	"encoding/xml"
	"fmt"
	"github.com/kataras/iris/v12"
	log "github.com/sirupsen/logrus"
)

type RequestObtainTicket struct {
	BuildDate       string `form:"buildDate" url:"buildDate"`
	BuildNumber     string `form:"buildNumber" url:"buildNumber"`
	ClientVersion   string `form:"clientVersion" url:"clientVersion"`
	HostName        string `form:"hostName" url:"hostName"`
	MachineID       string `form:"machineId" url:"machineId"`
	ProductCode     string `form:"productCode" url:"productCode"`
	ProductFamilyID string `form:"productFamilyId" url:"productFamilyId"`
	Salt            string `form:"salt" url:"salt"`
	Secure          string `form:"secure" url:"secure"`
	UserName        string `form:"userName" url:"userName"`
	Version         string `form:"version" url:"version"`
	VersionNumber   string `form:"versionNumber" url:"versionNumber"`
}
type ResponseObtainTicket struct {
	XMLName                  xml.Name `xml:"ObtainTicketResponse"`
	Text                     string   `xml:",chardata"`
	Action                   string   `xml:"action"`
	ConfirmationStamp        string   `xml:"confirmationStamp"`
	LeaseSignature           string   `xml:"leaseSignature"`
	Message                  string   `xml:"message"`
	ProlongationPeriod       string   `xml:"prolongationPeriod"`
	ResponseCode             string   `xml:"responseCode"`
	Salt                     string   `xml:"salt"`
	ServerLease              string   `xml:"serverLease"`
	ServerUid                string   `xml:"serverUid"`
	TicketId                 string   `xml:"ticketId"`
	TicketProps              string   `xml:"ticketProperties"`
	ValidationDeadlinePeriod string   `xml:"validationDeadlinePeriod"`
	ValidationPeriod         string   `xml:"validationPeriod"`
}

func ObtainTicketAction(context iris.Context) {
	var requestParsed RequestObtainTicket
	if err := context.ReadBody(&requestParsed); err != nil {
		context.StopWithError(iris.StatusBadRequest, err)
		return
	}
	log.WithFields(log.Fields{
		"platform":        "jetbrains",
		"type":            getProductType(requestParsed.ProductCode),
		"version":         requestParsed.Version,
		"versionNumber":   requestParsed.VersionNumber,
		"clientVersion":   requestParsed.ClientVersion,
		"hostname":        requestParsed.HostName,
		"username":        requestParsed.UserName,
		"productCode":     requestParsed.ProductCode,
		"productFamilyId": requestParsed.ProductFamilyID,
	}).Info("JetBrains 请求许可")
	var responseXML = ResponseObtainTicket{
		Action:             "NONE",
		ConfirmationStamp:  getConfirmationStamp(requestParsed.MachineID),
		LeaseSignature:     leaseSignature,
		Message:            "",
		ProlongationPeriod: "600000",
		ResponseCode:       "OK",
		Salt:               requestParsed.Salt,
		ServerLease:        leaseContent,
		ServerUid:          serverUid,
		TicketId:           getTicketId(),
		TicketProps:        fmt.Sprintf("licensee=%s\tlicenseType=1", requestParsed.UserName),
		//TicketProps:              "licensee=12345\tlicenseType=1",
		ValidationDeadlinePeriod: "-1",
		ValidationPeriod:         "600000",
	}

	licenseXML, err := xml.Marshal(responseXML)
	if err != nil {
		context.StopWithError(iris.StatusInternalServerError, err)
		return
	}
	context.ContentType("text/xml; charset=utf-8")
	context.WriteString(buildResponse(licenseXML))
}
